﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ARCam3D.Imaging;

namespace ARCam3D.UI
{
    using GDIColor = System.Drawing.Color;
    using XNAColor = Microsoft.Xna.Framework.Color;

    public static class GraphicsUtils
    {
        private static string contentPath = "Content";
        private static ContentManager contentManager = new ContentManager(ServiceProvider.Instance, contentPath);

        public static ContentManager ContentManager
        {
            get { return contentManager; }
        }

        unsafe public static Texture2D GetTextureFromImage(RGBImage image, GraphicsDevice graphicsDevice)
        {
            int bytesPerPixel = RGBImage.BytesPerPixel;
            int redOffset = RGBImage.RedOffset;
            int greenOffset = RGBImage.GreenOffset;
            int blueOffset = RGBImage.BlueOffset;

            int width = image.Width;
            int height = image.Height;
            long pointsCount = width * height;
           
            XNAColor[] textureData = new XNAColor[width * height];
            byte* imageDataPointer = (byte*)image.DataPtr.ToPointer();
            for (long point = 0; point < pointsCount; ++point, imageDataPointer += bytesPerPixel)
            {
                textureData[point].R = imageDataPointer[redOffset];
                textureData[point].G = imageDataPointer[greenOffset];
                textureData[point].B = imageDataPointer[blueOffset];
            }

            Texture2D texture = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color);
            texture.SetData(textureData);
            return texture;
        }

        public static Bitmap GetBitmapFromTexture(Texture2D texture)
        {
            int width = texture.Width;
            int height = texture.Height;

            XNAColor[] textureData = new XNAColor[width * height];
            texture.GetData<XNAColor>(textureData, 0, width * height);

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            BitmapData bitmapData = bitmap.LockBits(
                new System.Drawing.Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite,
                bitmap.PixelFormat);

            byte[] bitmapBytes = new byte[width * height * 3];

            

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    bitmapBytes[(y * width + x) * 3 + 2] = textureData[y * (width - 1) + x].R;
                    bitmapBytes[(y * width + x) * 3 + 1] = textureData[y * (width - 1) + x].G;
                    bitmapBytes[(y * width + x) * 3] = textureData[y * (width - 1) + x].B;
                }
            }

            Marshal.Copy(bitmapBytes, 0, bitmapData.Scan0, width * height * 3);

            bitmap.UnlockBits(bitmapData);

            //texture.SaveAsJpeg(new System.IO.FileStream("1.jpg", System.IO.FileMode.Create), 100, 100);
            //texture.SaveAsPng(new System.IO.FileStream("1.png", System.IO.FileMode.Create), 100, 100);

            return bitmap;

        }
    }
}
